<?php


namespace app\admin\logic;

use app\admin\error\AuthGroup as AuthGroupError;

class AuthGroupLogic extends AdminBase
{
    /**
     * @param $param
     * @return array|mixed
     */
    public static function getAuthGroupList($param)
    {
        $admin = Oauth::$userInfo;
        $where[] = ['status', '<>', '-1'];
        $where[] = ['module', '=', 'admin'];
        if($admin['id'] != 1){
            $where[] = ['admin_id', '=',$admin['id']];
        }

        !empty($param['name']) && $where[] = ['name','like', '%'.$param['name'].'%'];
        $limit = $param['pageSize'] ?? 10;
        $page = $param['page'] ?? 0;

        $list = self::model('AuthGroup')->getAuthGroupList($where, '*', $page, $limit);
        return empty($page) ? returnData($list) : $list;
    }

 /**
     * @param $param
     * @return array|mixed
     */
    public static function getNoPageAuthGroupList($param)
    {
        $admin = Oauth::$userInfo;
        $where[] = ['status', '<>', '-1'];
        $where[] = ['module', '=', 'admin'];
        if($admin['id'] != 1){
            $where[] = ['admin_id', '=',$admin['id']];
        }
        !empty($param['name']) && $where[] = ['name','like', '%'.$param['name'].'%'];


        $list = self::model('AuthGroup')->getNoPageMenuList($where, '*');
        return  returnData($list);
    }

    /**
     * @param $param
     * @return mixed
     */
    public static function saveAuthGroup($param)
    {
        self::paramSceneCheck(self::validate("authGroup"), empty($param['id']) ? 'add' : 'update', $param);
        $admin = Oauth::$userInfo;

        $where = empty($param['id']) ?
            [['module', '=', 'admin'], ['name', '=', $param['name']],['status','<>','-1']] :
            [['module', '=', 'admin'], ['id', '=', $param['id']]];
        $info = self::model('AuthGroup')->getAuthGroupInfo($where);
        empty($param['id']) ?
            !empty($info) && self::adminError(AuthGroupError::$authGroupNameError) :
            empty($info) && self::adminError(AuthGroupError::$infoError);

        $param['admin_id'] =  $admin['id'];
        $param['module'] = 'admin';

        $AuthGroupModel = self::model('AuthGroup');
        $res = empty($param['id']) ? $AuthGroupModel->addInfo($param) : $AuthGroupModel->updateInfo($where,$param);
        return returnData($res);
    }

    /**
     * @param $param
     * @return mixed
     */
    public static function updateAuthGroup($param)
    {

    }

    /**
     * @param $param
     * @return mixed
     */
    public static function deleteAuthGroup($param)
    {

        self::paramSceneCheck(self::validate("authGroup"), "delete", $param);
        $admin = Oauth::$userInfo;
        $where[] = ['id', '=', $param['id']];
        $authGroup = self::model('AuthGroup')->getInfo($where);
        empty($authGroup) && self::adminError(AuthGroupError::$infoError);
        $admin['id'] != $authGroup['admin_id'] && self::adminError(AuthGroupError::$parentError);
        $group_info =  self::model('AuthGroupAccess')->getInfo(['group_id'=>  $param['id']]);
        !empty($group_info) && self::adminError(AuthGroupError::$hasUserError);
        return self::model('AuthGroup')->getOneValue($where,DATA_STATUS_NAME, DATA_DELETE);;
    }


}
